using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Xml.Serialization;

namespace Rogue
{
    // A map of named terrainDefs 

    public sealed class TerrainDatabase
    {
        private Dictionary<string, TerrainDef> m_Database = new Dictionary<string, TerrainDef>();

        private TerrainDatabase()
        {
            ConsoleWindow.Instance.RegisterFunction("TERRAIN.LIST", CONSOLE_ListDefinitions);
        }

        #region Singleton Hackery
        public static TerrainDatabase Instance
            { get { return Nested.instance; } }

        class Nested
        {
            static Nested()
            {
                // Nothing
            }

            internal static readonly TerrainDatabase instance = new TerrainDatabase();
        }
        #endregion

        public TerrainDef GetDefinition(string a_sName)
        {
            if (m_Database.ContainsKey(a_sName.ToUpper()))
                return m_Database[a_sName.ToUpper()];
            else
            {
                throw new Exception("Cannot find terrain definition \"" + a_sName + "\"");
            }
        }

        public bool ContainsDefinition(string a_sName)
        {
            if (m_Database.ContainsKey(a_sName.ToUpper()))
                return true;
            else
                return false;
        }

        public void LoadDefinitions()
        {

            List<TerrainDef> definitions = RogueGame.Instance.Content.Load<List<TerrainDef>>("terraindefs");

            foreach (TerrainDef def in definitions)
            {
                m_Database.Add(def.Name.ToUpper(), def);
            }
        }

        #region Console Commands
        private void CONSOLE_ListDefinitions(string[] a_sArgs)
        {
            foreach (KeyValuePair<string, TerrainDef> def in m_Database)
            {
                ConsoleWindow.Instance.PrintError(def.Value.Name);
            }
        }
        #endregion
    }

   

}